package com.aidex.biz.service.impl;

import com.aidex.biz.domain.Taizhang003;
import com.aidex.biz.mapper.Taizhang003Mapper;
import com.aidex.biz.service.Taizhang003Service;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.sign.Md5Utils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 警示教育登记台Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-05-22
 */
@Service
@Transactional(readOnly = true)
public class Taizhang003ServiceImpl extends BaseServiceImpl<Taizhang003Mapper, Taizhang003> implements Taizhang003Service {
    private static final Logger log = LoggerFactory.getLogger(Taizhang003ServiceImpl.class);

    /**
     * 获取单条数据
     *
     * @param taizhang003 警示教育登记台
     * @return 警示教育登记台
     */
    @Override
    public Taizhang003 get(Taizhang003 taizhang003) {
        Taizhang003 dto = super.get(taizhang003);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 警示教育登记台id
     * @return 警示教育登记台
     */
    @Override
    public Taizhang003 get(String id) {
        Taizhang003 dto = super.get(id);
        return dto;
    }


    /**
     * 查询警示教育登记台列表
     *
     * @param taizhang003 警示教育登记台
     * @return 警示教育登记台
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<Taizhang003> findList(Taizhang003 taizhang003) {
        return super.findList(taizhang003);
    }

    /**
     * 分页查询警示教育登记台列表
     *
     * @param taizhang003 警示教育登记台
     * @return 警示教育登记台
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<Taizhang003> findPage(Taizhang003 taizhang003) {
        return super.findPage(taizhang003);
    }

    /**
     * 保存警示教育登记台
     *
     * @param taizhang003
     * @return 结果
     */
    @Override
    public boolean save(Taizhang003 taizhang003) {
        return super.save(taizhang003);
    }


    @Override
    @Transactional(readOnly = false)
    public String importData(List<Taizhang003> taizhang003s) {
        if (StringUtils.isNull(taizhang003s) || taizhang003s.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int updateNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Taizhang003 obj : taizhang003s) {
            try {
                if (StringUtils.isNotNull(obj.getName())) {
                    String id = Md5Utils.hash(obj.getName());
                    Taizhang003 taizhang003 = this.get(id);
                    obj.setId(id);
                    if (taizhang003 == null) {
                        obj.setDeptId(SecurityUtils.getDeptId());
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy",
                                Locale.ENGLISH).withZone(ZoneId.of("America" + "/Chicago"));
                        // 解析字符串为LocalDateTime
                        LocalDateTime dateTime = LocalDateTime.parse(obj.getToTime(), formatter);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
                        obj.setToTime(sdf.format(date));
                        obj.setIsNewRecord();
                        this.save(obj);
                        successNum++;
                    } else {
                        obj.setDeptId(SecurityUtils.getDeptId());
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy",
                                Locale.ENGLISH).withZone(ZoneId.of("America" + "/Chicago"));
                        // 解析字符串为LocalDateTime
                        LocalDateTime dateTime = LocalDateTime.parse(obj.getToTime(), formatter);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
                        obj.setToTime(sdf.format(date));
                        this.save(obj);
                        updateNum++;
                    }


                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "";
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确。");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "数据已全部导入成功！新增数据 " + successNum + " 条,修改数据：" + updateNum + " 条");
        }
        return successMsg.toString();

    }

    /**
     * 删除警示教育登记台信息
     *
     * @param taizhang003
     * @return 结果
     */
    @Override
    public boolean remove(Taizhang003 taizhang003) {
        return super.remove(taizhang003);
    }

    /**
     * 批量删除警示教育登记台
     *
     * @param ids 需要删除的警示教育登记台ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteTaizhang003ByIds(String[] ids) {
        return mapper.deleteTaizhang003ByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param taizhang003 警示教育登记台
     * @return 结果
     */
    @Override
    public int findMaxSort(Taizhang003 taizhang003) {
        return NumberUtils.nextOrder(mapper.findMaxSort(taizhang003));
    }
}
